package com.demo2do.fms.admin.web.controller.query;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.query.VehicleQuery;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.entity.vehicle.source.Vehicle;

/**
 * 车辆查询
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/vehicles")
public class VehicleQueryController {
    
    @Autowired
    private DataService<Vehicle> dataService;
    
    /**
     * 交通工具失去焦点查询
     * 
     * @param code
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET, params = "code" ,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onQuery(@RequestParam("code") String code, @RequestParam(value = "orgIds", required = false) List<Long> orgIds) {
        
        HttpResult httpResult = new HttpResult();
        
        Vehicle vehicle=null;
        
        if(CollectionUtils.isEmpty(orgIds)) {
            
             vehicle = dataService.load(Vehicle.class, code, "(code = :code OR name = :code OR barCode = :code)");
             
        } else {
             
             vehicle = dataService.load(Vehicle.class, code, orgIds , "(code = :code OR name = :code OR barCode = :code)");
             
        }
        
        if(vehicle != null){
            
            httpResult.success().data("id", vehicle.getId())
                                .data("code", vehicle.getCode())
                                .data("name", vehicle.getName());
            
            return httpResult;
        }
       
        return httpResult.fail("400").message("查询失败");
        
    }
    
    /**
     * 弹窗查询
     * 
     * @return
     */
    @RequestMapping("/query")
    public String modalQuery() {
        return "query/vehicle-query-modal";
    }
    
    /**
     * 
     * @param vehicleQuery
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "query" , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onModalQuery(VehicleQuery vehicleQuery,
                                                 HttpServletRequest request,
                                                 Page page) {
        
        HttpResult httpResult = new HttpResult();
        
        int totals = dataService.count(Vehicle.class, vehicleQuery.getFilter());
        
        if(totals > 0){
            
            List<Vehicle> vehicles = dataService.search(Vehicle.class, vehicleQuery, page.with(totals));
            
            httpResult.success().data("list", vehicles)
                                .data("page", page)
                                .data("queryString", WebUtils.extractQueryString(request, "query", "page"));
        } else {
            httpResult.fail("500").message("查询结果为空");
        }
        
        return httpResult;
        
    }
    
}
